package com.yswl.pri.picture;

import android.Manifest;
import android.app.Dialog;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.yswl.pri.R;
import com.yswl.pri.eventbus.PictureDialogEvent;
import com.yswl.pri.guide.adapter.MyAdapter;
import com.yswl.pri.picture.adapter.PictureDialogFragmentPagerAdapter;
import com.yswl.pri.picture.entry.ImageBean;
import com.yswl.pri.picture.glide.ImgUtils;
import com.yswl.pri.picture.net.PictrueSaveAsyTask;
import com.yswl.pri.picture.widgets.BottomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import yswl.com.klibrary.MApplication;
import yswl.com.klibrary.inteface.EventBusSetting;
import yswl.com.klibrary.permision.PermissionListener;
import yswl.com.klibrary.permision.PermissionUtil;
import yswl.com.klibrary.util.L;
import yswl.com.klibrary.util.MKeyBoardUtils;
import yswl.com.klibrary.util.ToastUtil;

/**
 * Created by kangpAdministrator on 2018/5/4 0004.
 * Emial kangpeng@yunhetong.net
 */

public class PictureDialog extends DialogFragment {
    private static final String TAG = PictureDialog.class.getSimpleName();
    List<View> imageViews;
    List<ImageBean> images;
    int postion;
    ViewPager mViewPager;
    PictureDialogFragmentPagerAdapter adapter;

    public static PictureDialog getInstance() {
        PictureDialog dialog = new PictureDialog();
        return dialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_picture);

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    /*-----------eventbus 注册---------------*/

    @Override
    public void onStart() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        super.onStart();
    }

    @Override
    public void onStop() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
        super.onStop();
    }


    @Subscribe(sticky = true)
    public void onEvent(PictureDialogEvent event) {
        images = event.getImages();
        postion = event.getPosition();
        if (imageViews == null)
            imageViews = getImageViews(images);
        adapter.setViews(images,imageViews);
        adapter.notifyDataSetChanged();
        mViewPager.setCurrentItem(postion);
        EventBus.getDefault().removeStickyEvent(event);

    }

    /*-----------eventbus 注册end---------------*/

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_fragment_picture, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewPager = view.findViewById(R.id.viewpager);
        adapter = new PictureDialogFragmentPagerAdapter(getActivity());
        mViewPager.setAdapter(adapter);

    }

    private List<View> getImageViews(List<ImageBean> images) {
        if (images == null) return null;
        List<View> imageViews = new ArrayList<>(images.size());
        LayoutInflater inflater = getLayoutInflater();
        View view;
        for (int i = 0; i < images.size(); i++) {
             view = inflater.inflate(R.layout.view_pager_adapter_image_item, null);
            imageViews.add(view);
        }
        return imageViews;
    }


    //权限请求回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {

        }
    }


}
